package de.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.preiscrawler.parser.amazon.AmazonParser;
import de.preisrobot.entity.Artikel;
import de.preisrobot.entity.CrawlerLinkUrl;
import de.preisrobot.persistence.dao.ArtikelDao;
import de.preisrobot.service.GrabberService;

@Controller
public class JSONArtikelController {

	@Resource(name="ArtikelDao")
	ArtikelDao dao ;  
	@Resource(name="amazonGrabberService")
	GrabberService amazonGrabberService;
	
	public JSONArtikelController()
	{
		System.out.println("\n\n HARRRRRRRRRAAAAAA\n\n");
	}
	
	@RequestMapping(value="find/{name}" ,method=RequestMethod.GET)
	public @ResponseBody List<Artikel> find (@PathVariable("name") String name)
	{
		System.out.println("\n\n  SUCHE:"+name+"\n\n");
		List<Artikel> find = dao.findAll("%"+name+"%");
		
		return find;
	}
	
	@RequestMapping(value="grep" ,method=RequestMethod.POST)
	public @ResponseBody List<Artikel> grep (@RequestBody   String url)
	{
		System.out.println("\ngrep SUCHE:"+url+"\n\n");
		CrawlerLinkUrl sourceUrlString = new CrawlerLinkUrl();
		sourceUrlString.setLink(url);
		sourceUrlString.setAnbieterId(0);
		sourceUrlString.setCategoryId(0);
		amazonGrabberService.setParser(new AmazonParser());
		List<Artikel> list = amazonGrabberService.grapUrl(sourceUrlString );
		 
		return list;
	}
	
	
	
 }
